package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DecimalCalculateUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcProjectCbpj;
import com.ruoyi.gcgl.domain.GcProjectCbpjitem;
import com.ruoyi.gcgl.service.IGcProjectCbpjService;
import com.ruoyi.gcgl.service.IGcProjectCbpjitemService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcSendTipService;

/**
 * 项目成本预估Controller
 * 
 * @author ruoyi
 * @date 2022-11-27
 */
@Controller
@RequestMapping("/gcgl/cbpj")
public class GcProjectCbpjController extends BaseController
{
    private String prefix = "gcgl/cbpj";

    @Autowired
    private IGcProjectCbpjService gcProjectCbpjService;
    @Autowired
    private IGcProjectCbpjitemService gcProjectCbpjitemService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private IGcSendTipService gcSendTipService;

    @RequiresPermissions("gcgl:cbpj:view")
    @GetMapping()
    public String cbpj()
    {
        return prefix + "/cbpj";
    }

    /**
     * 查询项目成本预估列表
     */
    @RequiresPermissions("gcgl:cbpj:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcProjectCbpj gcProjectCbpj)
    {
        startPage();
        gcProjectCbpj.setDeleteFlag(1);
        List<GcProjectCbpj> list = gcProjectCbpjService.selectGcProjectCbpjList(gcProjectCbpj);
        
        for(GcProjectCbpj obj:list){
        	GcProject project = gcProjectService.selectGcProjectById(obj.getGcProjectid());
        	obj.setStatus(project.getStatus());
        }
        
        return getDataTable(list);
    }

    /**
     * 导出项目成本预估列表
     */
    @RequiresPermissions("gcgl:cbpj:export")
    @Log(title = "项目成本预估", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcProjectCbpj gcProjectCbpj)
    {
    	gcProjectCbpj.setDeleteFlag(1);
    	if(getSysUser().isShow()){
            gcProjectCbpj.setCreateUser(getLoginName());
        }
        List<GcProjectCbpj> list = gcProjectCbpjService.selectGcProjectCbpjList(gcProjectCbpj);
        ExcelUtil<GcProjectCbpj> util = new ExcelUtil<GcProjectCbpj>(GcProjectCbpj.class);
        return util.exportExcel(list, "项目成本预估数据");
    }

    /**
     * 新增项目成本预估
     */
    @GetMapping("/add/{gcProjectid}")
    public String add(ModelMap mmap,@PathVariable("gcProjectid") Long gcProjectid)
    {
    	GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectid);
    	mmap.put("gcProject", gcProject);
    	mmap.put("pgsj", DateUtils.dateTime(new Date()));
    	mmap.put("xmfzr", getLoginName());
        return prefix + "/add";
    }

    /**
     * 新增保存项目成本预估
     */
    @RequiresPermissions("gcgl:cbpj:add")
    @Log(title = "项目成本预估", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcProjectCbpj gcProjectCbpj){
    	//head
    	gcProjectCbpj.setStatus(0);
    	gcProjectCbpj.setDeleteFlag(1);
    	gcProjectCbpj.setCreateAt(new Date());
    	gcProjectCbpj.setUpdateAt(new Date());
    	gcProjectCbpj.setCreateUser(getLoginName());
    	gcProjectCbpj.setUpdateUser(getLoginName());
    	gcProjectCbpjService.insertGcProjectCbpj(gcProjectCbpj);
    	
    	double cbhj = 0.0;
    	//item
    	int i=1;
    	for(GcProjectCbpjitem item : gcProjectCbpj.getItem()){
    		item.setDeleteFlag(1);
    		item.setCreateAt(new Date());
    		item.setUpdateAt(new Date());
    		item.setCreateUser(getLoginName());
    		item.setUpdateUser(getLoginName());
    		item.setGcProjectid(gcProjectCbpj.getGcProjectid());
    		item.setCbpjId(gcProjectCbpj.getId());
    		item.setXuh(i++);
    		cbhj+=Double.parseDouble(item.getXjjg().toString());
    		
    		gcProjectCbpjitemService.insertGcProjectCbpjitem(item);
    	}
    	GcProjectCbpj head = new GcProjectCbpj();
    	head.setId(gcProjectCbpj.getId());
    	head.setCbhj(Double.valueOf(cbhj));
    	gcProjectCbpjService.updateGcProjectCbpj(head);
    	
        return toAjax(1);
    }

    /**
     * 修改项目成本预估
     */
    @RequiresPermissions("gcgl:cbpj:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectCbpj gcProjectCbpj = gcProjectCbpjService.selectGcProjectCbpjById(id);
        List<GcProjectCbpjitem> itemlis= gcProjectCbpjitemService.selectGcProjectCbpjitemByCbpjId(id);
        gcProjectCbpj.setItem(itemlis);
        mmap.put("gcProjectCbpj", gcProjectCbpj);
        
        GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectCbpj.getGcProjectid());
    	mmap.put("gcProject", gcProject);
        return prefix + "/edit";
    }
    
    /**
     * 修改项目成本预估
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectCbpj gcProjectCbpj = gcProjectCbpjService.selectGcProjectCbpjById(id);
        List<GcProjectCbpjitem> itemlis= gcProjectCbpjitemService.selectGcProjectCbpjitemByCbpjId(id);
        gcProjectCbpj.setItem(itemlis);
        mmap.put("gcProjectCbpj", gcProjectCbpj);
        return prefix + "/detail";
    }

    /**
     * 修改保存项目成本预估
     */
    @RequiresPermissions("gcgl:cbpj:edit")
    @Log(title = "项目成本预估", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcProjectCbpj gcProjectCbpj){
    	
    	try {
    		gcProjectCbpjitemService.deleteGcProjectCbpjitemById(gcProjectCbpj.getId());
    		
    		double cbhj = 0.0;
        	//item
    		int i=1;
        	for(GcProjectCbpjitem item : gcProjectCbpj.getItem()){
        		item.setDeleteFlag(1);
        		item.setUpdateAt(new Date());
        		item.setCreateUser(getLoginName());
        		item.setUpdateUser(getLoginName());
        		item.setGcProjectid(gcProjectCbpj.getGcProjectid());
        		item.setCbpjId(gcProjectCbpj.getId());
        		item.setXuh(i++);
        		gcProjectCbpjitemService.insertGcProjectCbpjitem(item);
        		
        		cbhj+=Double.parseDouble(item.getXjjg().toString());
        	}
        	
        	gcProjectCbpj.setCbhj(Double.valueOf(cbhj));
        	gcProjectCbpj.setStatus(0);
        	gcProjectCbpj.setXmjlsh("");
        	gcProjectCbpj.setZjlsh("");
        	gcProjectCbpjService.updateGcProjectCbpj(gcProjectCbpj);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return toAjax(0);
		}
    	
        return toAjax(1);
    }

    /**
     * 删除项目成本预估
     */
    @RequiresPermissions("gcgl:cbpj:remove")
    @Log(title = "项目成本预估", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        return toAjax(gcProjectCbpjService.deleteGcProjectCbpjByIds(ids));
    }
    
    /**
     * 总经理审核
     * @return
     */
    @RequiresPermissions("gcgl:cbpj:gcpsview")
    @GetMapping("/gcps")
    public String cbpjgcps(){
        return prefix + "/cbpjgcps";
    }
    
    /**
     * 查询项目成本预估列表总经理审核
     */
    @PostMapping("/gcpslist")
    @ResponseBody
    public TableDataInfo gcpslist(GcProjectCbpj gcProjectCbpj){
        startPage();
        gcProjectCbpj.setDeleteFlag(1);
        gcProjectCbpj.setStatus(0);
        List<GcProjectCbpj> list = gcProjectCbpjService.selectGcProjectCbpjList(gcProjectCbpj);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/gcyes")
    @ResponseBody
    public AjaxResult gcyes(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectCbpj obj = new GcProjectCbpj();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(1);
    		obj.setXmjlsh(getLoginName());
    		obj.setXmjlshAt(new Date());
    		obj.setSpyj(spyj);
        	gcProjectCbpjService.updateGcProjectCbpj(obj);
        	
        	GcProjectCbpj gcProjectCbpj = gcProjectCbpjService.selectGcProjectCbpjById(Long.parseLong(id[i]));
        	//发送消息通知
//        	gcSendTipService.sendTip("cbyg", gcProjectCbpj.getXmmc(), gcProjectCbpj.getOrderNo(), 
//        			gcProjectCbpj.getCreateUser(), DateUtils.dateTime(gcProjectCbpj.getCreateAt()), 
//        			gcProjectCbpj.getStatus().toString(), null, gcProjectCbpj.getId().toString());
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/gcno")
    @ResponseBody
    public AjaxResult gcno(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectCbpj obj = new GcProjectCbpj();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(2);
    		obj.setXmjlsh(getLoginName());
    		obj.setXmjlshAt(new Date());
    		obj.setSpyj(spyj);
    		gcProjectCbpjService.updateGcProjectCbpj(obj);
    	}
    	return toAjax(1);
    }
    
    /**
     * 总经理审核
     * @return
     */
    @RequiresPermissions("gcgl:cbpj:psview")
    @GetMapping("/ps")
    public String cbpjps(){
        return prefix + "/cbpjps";
    }
    
    /**
     * 查询项目成本预估列表总经理审核
     */
    @PostMapping("/pslist")
    @ResponseBody
    public TableDataInfo pslist(GcProjectCbpj gcProjectCbpj){
        startPage();
        gcProjectCbpj.setDeleteFlag(1);
        gcProjectCbpj.setStatus(1);
        List<GcProjectCbpj> list = gcProjectCbpjService.selectGcProjectCbpjList(gcProjectCbpj);
        return getDataTable(list);
    }
    
    @Log(title = "通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlyes")
    @ResponseBody
    public AjaxResult zjlyes(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectCbpj obj = new GcProjectCbpj();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(3);
    		obj.setZjlsh(getLoginName());
    		obj.setZjlshAt(new Date());
    		obj.setSpyj(spyj);
        	gcProjectCbpjService.updateGcProjectCbpj(obj);
        	
    	}
    	
    	return toAjax(1);
    }
    
    @Log(title = "不通过", businessType = BusinessType.UPDATE)
    @PostMapping("/zjlno")
    @ResponseBody
    public AjaxResult zjlno(String ids,String spyj, ModelMap mmap){
    	String[] id = Convert.toStrArray(ids);
    	for(int i=0;i<id.length;i++){
    		GcProjectCbpj obj = new GcProjectCbpj();
    		obj.setId(Long.parseLong(id[i]));
    		obj.setStatus(4);
    		obj.setZjlsh(getLoginName());
    		obj.setZjlshAt(new Date());
    		obj.setSpyj(spyj);
    		gcProjectCbpjService.updateGcProjectCbpj(obj);
    		
    	}
    	return toAjax(1);
    }
    
    /**
     * 修改项目成本预估
     */
    @RequiresPermissions("gcgl:cbpj:cbygprint")
    @GetMapping("/cbygprint/{id}")
    public String cbygprint(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectCbpj gcProjectCbpj = gcProjectCbpjService.selectGcProjectCbpjById(id);
        Double h_zbs = DecimalCalculateUtil.div(Double.parseDouble(gcProjectCbpj.getLryg().toString()),
    			Double.parseDouble(gcProjectCbpj.getCbhj().toString()), 4);
        gcProjectCbpj.setZbs(DecimalCalculateUtil.mul(h_zbs, 100)+"%");
        
        List<GcProjectCbpjitem> itemlis= gcProjectCbpjitemService.selectGcProjectCbpjitemByCbpjId(id);
        for(GcProjectCbpjitem item:itemlis){
        	Double zbs = DecimalCalculateUtil.div(Double.parseDouble(item.getXjjg().toString()),
        			Double.parseDouble(gcProjectCbpj.getCbhj().toString()), 4);
        	item.setZbs(DecimalCalculateUtil.mul(zbs, 100)+"%");
        }
        gcProjectCbpj.setItem(itemlis);
        mmap.put("gcProjectCbpj", gcProjectCbpj);
        return prefix + "/cbygprint";
    }
    
    /**
     * 工程经理审核界面
     */
    @GetMapping("/cbyggcsp/{id}")
    public String cbyggcsp(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectCbpj gcProjectCbpj = gcProjectCbpjService.selectGcProjectCbpjById(id);
        Double h_zbs = DecimalCalculateUtil.div(Double.parseDouble(gcProjectCbpj.getLryg().toString()),
    			Double.parseDouble(gcProjectCbpj.getCbhj().toString()), 4);
        gcProjectCbpj.setZbs(DecimalCalculateUtil.mul(h_zbs, 100)+"%");
        
        List<GcProjectCbpjitem> itemlis= gcProjectCbpjitemService.selectGcProjectCbpjitemByCbpjId(id);
        for(GcProjectCbpjitem item:itemlis){
        	Double zbs = DecimalCalculateUtil.div(Double.parseDouble(item.getXjjg().toString()),
        			Double.parseDouble(gcProjectCbpj.getCbhj().toString()), 4);
        	item.setZbs(DecimalCalculateUtil.mul(zbs, 100)+"%");
        }
        gcProjectCbpj.setItem(itemlis);
        mmap.put("gcProjectCbpj", gcProjectCbpj);
        return prefix + "/cbyggcsp";
    }
    
    /**
     * 总经理审批页面
     */
    @GetMapping("/cbygzjlsp/{id}")
    public String cbygzjlsp(@PathVariable("id") Long id, ModelMap mmap){
        GcProjectCbpj gcProjectCbpj = gcProjectCbpjService.selectGcProjectCbpjById(id);
        Double h_zbs = DecimalCalculateUtil.div(Double.parseDouble(gcProjectCbpj.getLryg().toString()),
    			Double.parseDouble(gcProjectCbpj.getCbhj().toString()), 4);
        gcProjectCbpj.setZbs(DecimalCalculateUtil.mul(h_zbs, 100)+"%");
        
        List<GcProjectCbpjitem> itemlis= gcProjectCbpjitemService.selectGcProjectCbpjitemByCbpjId(id);
        for(GcProjectCbpjitem item:itemlis){
        	Double zbs = DecimalCalculateUtil.div(Double.parseDouble(item.getXjjg().toString()),
        			Double.parseDouble(gcProjectCbpj.getCbhj().toString()), 4);
        	item.setZbs(DecimalCalculateUtil.mul(zbs, 100)+"%");
        }
        gcProjectCbpj.setItem(itemlis);
        mmap.put("gcProjectCbpj", gcProjectCbpj);
        return prefix + "/cbygzjlsp";
    }
}